/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.dao.daoImpl;

import com.miracleteam.miracle.dao.CommentCauHoiGianHangDAO;
import com.miracleteam.miracle.entities.Commentcauhoigianhang;
import com.miracleteam.miracle.utils.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author DangKhoa
 */
public class CommentCauHoiGianHangDAOImpl implements CommentCauHoiGianHangDAO {
    Logger logger= Logger.getLogger(CommentCauHoiGianHangDAOImpl.class);
     public String PhatSinhMaCauTraLoi() {
          String kq="AS-";
        Date date = new Date();
        kq+=String.valueOf(date.getDay());
        kq+=String.valueOf(date.getMonth());
        kq+=String.valueOf(date.getYear());
        kq+="-";
        kq+=String.valueOf(date.getHours());
        kq+=String.valueOf(date.getMinutes());
        kq+=String.valueOf(date.getSeconds());
        
        return kq;
    }
    public List LayDSCommentCauHoi(String maCH) {
          List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
           
            String hql = "select cm from Commentcauhoigianhang cm where cm.daXoa = 0 and cm.cauhoichocuahang.maCauHoiChoCuaHang=:maCH order by cm.lastModified DESC";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maCH",maCH);
            query.setFirstResult(0);
            query.setMaxResults(9);
           
            ds = query.list();

        } catch (Exception e) {
            logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        finally{
            session.close();
        }
        return ds;
    }

    public int ThemCommentCauHoi(Commentcauhoigianhang cm) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (cm== null) {
            return 0;
        }
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(cm);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
            System.err.println(ex);
        } finally {
            session.close();
        }
        return 1;
    }
    
}
